package com.zhongpengcheng.blog.util;

import com.zhongpengcheng.blog.dao.pojo.db.UserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;

/**
 * shiro工具类，封装鉴权相关操作
 *
 * @author zhongpengcheng
 * @date 2021/08/19 12:58
 **/
public class ShiroUtils {

    private ShiroUtils() {
    }

    /**
     * 获取当前登录用户
     * @return 系统当前登录用户
     */
    public static UserDO getCurrentSysUser() {
        return (UserDO) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 从{@link AuthenticationInfo}中获取user对象
     * @param info 授权信息
     * @return 授权信息中的user对象
     * @see org.apache.shiro.authc.SimpleAuthenticationInfo
     */
    public static UserDO getSysUserFromInfo(AuthenticationInfo info) {
        return (UserDO) info.getPrincipals().getPrimaryPrincipal();
    }
}
